<?php

namespace plugins\setting\controller; 

use cmf\controller\PluginAdminBaseController;//引入此类

use think\Db;

use think\Validate;

use plugins\setting\controller\SettingController;
use plugins\setting\model\SettingInstallPackageModel;



class AdminIndexController extends PluginAdminBaseController

{
    /**
     * 热更新模块 菜单注解
     * @adminMenu(
     *      'name'      =>  '热更新',
     *      'parent'    =>  'admin/Plugin/default',
     *      'display'   =>  true,
     *      'hasView'   =>  true,
     *      'order'     =>  9999,
     *      'icon'      =>  '',
     *      'remark'    =>  '热更新',
     *      'param'     =>  ''
     * )
     */
    public function index()

    {
    	return $this->fetch();
        return ( new SettingController() )->index();

    } 

    /**
     * 安装包上传
     */
    public function installpageupload()
    {
    	$param = $this->request->param();

    	$validate = new Validate( [
    		[ 'version' , 'require' , '版本不能为空' ],
    		[ 'describle' , 'require' , '描述不能为空'],
    		[ 'type' , 'require' , '请选择类型'],
    	] );

        if( empty( $_FILES ) ){
            return api_json_echo( false , '请上传安装包' );
        }

		//模块的标识
		$module = $param['_plugin'];

		//文件夹的标识
		$info = 'installpackage';

		//获取上传的文件
        $file = $_FILES['file'];

        $rule = [ 
            'size' => 1024*1024*100,    //100mb
            'ext' =>['apk' , 'ipa' , 'wgt' ],
        ];        

        $res = fileUpload($file,$module,$info,$rule);
        if($res['status'] == true){
        	return json(['save'=>$res['data'],'info'=>$info,'code'=>200,'message'=>'上传成功']);
        }else{
        	return json(['code'=>-1,'message'=>'上传失败']);
        }



        // $path = '/upload/setting/installpackage';
        // $rule = [ 
        //     'size' => 1024*1024*100,    //100mb
        //     'ext' =>['apk' , 'ipa' , 'wgt' ],
        // ];
        
        // $res = uploadImg( $_FILES [ 'file' ] , $path , $rule );
        
        // if( !$res[ 'status' ] ){
        //     return api_json_echo( false , '上传失败' , $res [ 'message' ] , $res [ 'code' ] );
        // }

        // //保存数据
        // $param [ 'download_addr' ] = $res [ 'data' ];
        // $res = ( new SettingInstallPackageModel() )->allowField( true )->isUpdate(false)->save( $param );
        // if( !$res ){
        // 	return api_json_echo( false , '数据保存失败,请稍后再试' );
        // }
        // return api_json_echo( true , '上传成功' ,  $res [ 'data' ] );
    }


    public function saveinfo()
    {
    	$param = $this->request->post();

    	$param['add_time'] = time();

    	$res = ( new SettingInstallPackageModel )->allowField(true)->save($param);

    	if($res){
    		return json(['code'=>200,'message'=>'保存成功']);
    	}else{
    		return json(['code'=>-1,'message'=>'保存失败']);
    	}

    }

}



